<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
<!--      <el-form-item label="品牌id" prop="brandId">-->
<!--        <el-input-->
<!--          v-model="queryParams.brandId"-->
<!--          placeholder="请输入品牌id"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="品牌名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入品牌名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="显示状态" prop="showStatus">
        <el-select
          v-model="queryParams.showStatus"
          placeholder="请选择显示状态"
          clearable
        >
          <el-option
            v-for="dict in sys_show_hide"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索 </el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['product:brand:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:brand:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:brand:remove']"
          >删除
        </el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="list" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="品牌id" align="center" prop="brandId" /> -->
      <el-table-column label="品牌名" align="center" prop="name" />
      <el-table-column label="品牌logo" align="center" prop="logo">
        <template #default="scope">
          <image-preview :src="scope.row.logo" :width="50" :height="50" />
        </template>
      </el-table-column>
      <el-table-column label="介绍" align="center" prop="descript" />
      <el-table-column label="显示状态" align="center" prop="showStatus">
        <template #default="scope">
          <dict-tag :options="sys_show_hide" :value="scope.row.showStatus" />
        </template>
      </el-table-column>
      <el-table-column label="检索首字母" align="center" prop="firstLetter" />
      <el-table-column label="排序" align="center" prop="sort" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            link
            icon="Edit"
            @click="handleConnect(scope.row)"
            v-hasPermi="['product:brand:edit']"
            >关联
          </el-button>
          <el-button
            link
            icon="Edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:brand:edit']"
            >修改
          </el-button>
          <el-button
            link
            icon="Delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:brand:remove']"
            >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品品牌对话框 -->
    <el-dialog :title="title" v-model="open" width="550px" append-to-body>
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <!-- <el-form-item label="品牌id" prop="brandId">
          <el-input v-model="form.brandId" placeholder="请输入品牌id" />
        </el-form-item> -->
        <el-form-item label="品牌名" prop="name">
          <el-input v-model="form.name" placeholder="请输入品牌名" />
        </el-form-item>
        <el-form-item label="品牌logo" prop="logo">
          <image-upload v-model="form.logo" />
        </el-form-item>
        <el-form-item label="介绍" prop="descript">
          <el-input v-model="form.descript" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="显示状态" prop="showStatus">
          <el-radio-group v-model="form.showStatus">
            <el-radio
              v-for="dict in sys_show_hide"
              :key="dict.value"
              :label="parseInt(dict.value)"
              >{{ dict.label }}
            </el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="检索首字母" prop="firstLetter">
          <el-input v-model="form.firstLetter" placeholder="请输入检索首字母" />
        </el-form-item>
        <el-form-item label="排序" prop="sort">
          <el-input v-model="form.sort" placeholder="请输入排序" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <!-- 关联分类 -->
    <el-dialog
      title="关联分类"
      destroy-on-close
      v-model="connectOpen"
      width="500px"
      :close-on-click-modal="false"
    >
      <relation :brand-id="form.brandId" />
    </el-dialog>
  </div>
</template>

<script setup>
import {
  listBrand,
  getBrand,
  delBrand,
  addBrand,
  updateBrand,
} from "@/api/mall/server/product/brand";
import { listTreeCategories } from "@/api/mall/server/product/cate";
import {
  listBrand2Category,
  addBrand2Category,
} from "@/api/mall/server/product/relation";
import { reactive, ref, toRefs, getCurrentInstance } from "vue";
import relation from "./relation.vue";
// vue3中获取this的方法
const { proxy } = getCurrentInstance();
const { sys_show_hide } = proxy.useDict("sys_show_hide");
// 查询list
const total = ref(0);
const loading = ref(false);
const list = ref([]);
// 单选多选
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
// 对话弹窗
const title = ref("");
const open = ref(false);
// 查询参数和表单
const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {},
});
const { queryParams, form, rules } = toRefs(data);
// 列表查询
const getList = async () => {
  loading.value = true;
  const res = await listBrand(queryParams.value);
  list.value = res.rows;
  total.value = res.total;
  loading.value = false;
};
// 取消
const cancel = () => {
  open.value = false;
  reset();
};

// 表单重置
const reset = () => {
  form.value = {
    brandId: undefined,
    name: undefined,
    logo: undefined,
    descript: undefined,
    showStatus: undefined,
    firstLetter: undefined,
    sort: undefined,
  };
};

// 搜索
const handleQuery = () => {
  queryParams.value.pageNum = 1;
  getList();
};

// 表单重置
const resetQuery = () => {
  proxy.resetForm("queryRef");
  handleQuery();
};

// 多选框选中数据
const handleSelectionChange = (selection) => {
  ids.value = selection.map((item) => item.brandId);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
};

// 添加弹窗
const handleAdd = (row) => {
  reset();
  open.value = true;
  title.value = "添加商品品牌";
};
// 修改操作
const handleUpdate = async (row) => {
  reset();
  const brandId = row.brandId || ids.value;
  const res = await getBrand(brandId);
  form.value = res.data;
  open.value = true;
  title.value = "修改商品品牌";
};
// 表单提交
const submitForm = () => {
  proxy.$refs["formRef"].validate((valid) => {
    if (valid) {
      if (form.value.brandId != undefined) {
        updateBrand(form.value).then((res) => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addBrand(form.value).then((res) => {
          proxy.$modal.msgSuccess("添加成功");
          open.value = false;
          getList();
        });
      }
    }
  });
};

// 删除
const handleDelete = (row) => {
  const ids = row.brandId || ids.value;
  proxy.$modal
    .confirm(`是否确认删除编号位${ids}的数据项?`)
    .then((res) => {
      return delBrand(ids);
    })
    .then(() => {
      getList();
      proxy.$modal.msgSuccess("删除成功");
    })
    .catch(() => {});
};
// 关联分类
const connectOpen = ref(false);
const brand2CateList = ref([]);
const selectedCate = ref();
const handleConnect = async (row) => {
  connectOpen.value = true;
  // const res = await listBrand2Category(row.brandId);
  // brand2CateList.value = res.rows;
  form.value = JSON.parse(JSON.stringify(row));
  console.log(form.value);
};

// 获取关联
const getBrand2CateList = async (row) => {
  listBrand2Category({ brandId: row.brandId }).then((res) => {
    brand2CateList.value = res.rows;
  });
};

// 添加关联
const isAddRelation = ref(false);
const addBrand2Cate = async () => {
  console.log("barndId", form.value);
  const res = await addBrand2Category(form.value.brandId, selectedCate.value);
  isAddRelation.value = false;
  getBrand2CateList(form.value);
};
const lv1Categories = ref([]);
const getCategories = async () => {
  const res = await listTreeCategories();
  lv1Categories.value = res.rows;
};

getCategories();
getList();
</script>
